/**
 * 
 */
package datamodel.rucksack;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * class which represents the rucksack
 * 
 * @author Jakob Karolus, Kevin Munk
 * @version 1.0
 * 
 */
public class Rucksack {

	private List<RucksackObject> elements;
	private int capacity;
	private int weight;
	private int value;

	/**
	 * constructor
	 * 
	 * @param capacity
	 *            the max capacity of this rucksack
	 * 
	 */
	public Rucksack(int capacity) {
		this.capacity = capacity;
		elements = new ArrayList<RucksackObject>();
		weight = 0;
		value = 0;
	}

	public Rucksack(Rucksack rucksack) {
		this.capacity = rucksack.getCapacity();
		
		elements = new ArrayList<RucksackObject>();
		elements.addAll(rucksack.getElements());
		weight = rucksack.getWeight();
		value = rucksack.getValue();
	}

	/**
	 * tries to add this RucksackObject to the rucksack
	 * 
	 * @param o
	 *            the RucksackObject to insert
	 * @return true if the object fits into the rucksack; otherwise false
	 */
	public boolean insert(RucksackObject o) {
		if (o.getWeight() <= capacity - weight) {
			elements.add(o);
			weight += o.weight;
			value += o.value;
			return true;
		} else
			return false;
	}

	/**
	 * Versucht ein Element aus dem Rucksack zu entfernen
	 * 
	 * @param o
	 *            das RucksackObject, dass entfernt werden soll
	 * @return true wenn es erfolgreich entfernt werden konnte sonst false.
	 */
	public boolean remove(RucksackObject o) {
		if (elements.contains(o)) {
			elements.remove(o);
			weight -= o.weight;
			value -= o.value;
			return true;
		} else
			return false;
	}

	public int getWeight() {
		return weight;
	}

	public int getValue() {
		return value;
	}

	public int getCapacity() {
		return capacity;
	}

	public int getSize() {
		return elements.size();
	}

	public Collection<RucksackObject> getElements() {
		return elements;
	}

	@Override
	public boolean equals(Object object) {
		if (object.getClass() != Rucksack.class)
			return false;

		Rucksack rucksack = (Rucksack) object;
		if (rucksack.getCapacity() == this.capacity
				&& rucksack.getWeight() == this.weight
				&& rucksack.getValue() == this.value
				&& rucksack.getElements().containsAll(elements))
			return true;
		else
			return false;
	}
	
	@Override
	public String toString() {
		return "Capacity: " + capacity + "\nWeight: " + weight;
	}
}
